import Notify from '@/../static/vant/notify/notify'
import Fly from 'flyio/dist/npm/wx'
let fly = new Fly()

fly.config.baseURL = 'http://192.168.137.1:8050'
/**
 *  定义公共headers
 */
fly.config.headers = {xx: 5, bb: 6, dd: 7}
/**
 *设置超时
 */
fly.config.timeout = 10000

/**
 * 功能描述：Http Request 拦截器
fly.interceptors.request.use((config) => {
  config.headers['X-Tag'] = 'flyio'
  return config
}, (error) => {
  Notify('请求参数错误')
  return Promise.reject(error)
})
 */
fly.interceptors.request.use((config, promise) => {
  config.headers['Content-Type'] = 'application/json'
  console.log(fly.interceptors.request)
  return config
}, (error) => {
  Notify('请求参数错误')
  return Promise.reject(error)
})

/**
 * 功能描述：Http Response 拦截器
 */
fly.interceptors.response.use((res) => {
  let isTips = res.config && (res.config.showMessage === undefined || res.config.showMessage === true)
  if (res.status !== 200) {
    isTips && Notify(`请求异常：${res.message}`, 10)
    return Promise.reject(res)
  } else {
    if (res.data.code !== 200) {
      isTips && Notify(`服务器返回异常：${res.data.message}`, 10)
      return Promise.reject(res)
    } else {
      return Promise.resolve(res.data)
    }
  }
}, (error) => {
  Notify(`服务器返回异常：${error}`, 10)
  return Promise.reject(error)
})

export default {
  get (url, params, config) {
    let configObj = {}
    if (config !== undefined) {
      Object.assign(configObj, config)
    }
    if (params !== undefined) {
      configObj['params'] = params
    }
    return fly.get(url, configObj)
  },
  delete (url, params, config) {
    let configObj = {}
    if (config !== undefined) {
      Object.assign(configObj, config)
    }
    if (params !== undefined) {
      configObj['params'] = params
    }
    return fly.delete(url, configObj)
  },
  post (url, data, config) {
    console.log('post:')
    console.log(data)
    return fly.post(url, data, config)
  },
  put (url, data, config) {
    return fly.put(url, data, config)
  },
  patch (url, data, config) {
    return fly.patch(url, data, config)
  }
}
